cmake_minimum_required (VERSION 3.16.3)
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
  cmake_policy(SET CMP0135 NEW)
endif()
project(optimization C CXX ASM)

include(CheckCXXCompilerFlag)

find_package(benchmark QUIET)

if (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
  enable_language(ASM_MASM)
endif()

OPTION(ENABLE_WERROR "Build with -Werror" OFF)

set (CMAKE_CXX_STANDARD 11)

include(FetchContent)
FetchContent_Declare(
  googletest
  # GoogleTest 1.11.0
  URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

include_directories("common")

if(CMAKE_CXX_COMPILER_ID MATCHES Clang OR CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES AppleClang)
  add_compile_options(-Wall -Winline -pedantic -march=haswell)
  add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES Clang OR CMAKE_CXX_COMPILER_ID MATCHES AppleClang)
  add_compile_options(-Wno-unused-command-line-argument)
endif()

if (ENABLE_WERROR)
  add_compile_options(-Werror)
endif()

enable_testing()
add_subdirectory(common)
add_subdirectory(chap5/ex15)
add_subdirectory(chap7/ex3)
add_subdirectory(chap7/ex4)
add_subdirectory(chap7/ex5)
add_subdirectory(chap7/ex6)
add_subdirectory(chap8/ex1)
add_subdirectory(chap8/ex2)
add_subdirectory(chap8/ex3)
add_subdirectory(chap8/ex4)
add_subdirectory(chap8/ex5)
add_subdirectory(chap8/ex6)
add_subdirectory(chap8/ex7)
add_subdirectory(chap8/ex9)
add_subdirectory(chap8/ex10)
add_subdirectory(chap8/ex11)
add_subdirectory(chap8/ex12)
add_subdirectory(chap15/ex1)
add_subdirectory(chap15/ex2)
add_subdirectory(chap15/ex3)
add_subdirectory(chap15/ex6)
add_subdirectory(chap15/ex7)
add_subdirectory(chap15/ex8)
add_subdirectory(chap15/ex9)
add_subdirectory(chap15/ex10)
add_subdirectory(chap15/ex12)
add_subdirectory(chap15/ex14)
add_subdirectory(chap15/ex16)
add_subdirectory(chap15/ex17)
add_subdirectory(chap15/ex18)
add_subdirectory(chap15/ex19)
add_subdirectory(chap15/ex20)
add_subdirectory(chap15/ex21)
add_subdirectory(chap15/ex22)
add_subdirectory(chap15/ex23)
add_subdirectory(chap15/ex24)
add_subdirectory(chap15/ex25)
add_subdirectory(chap15/ex26)
add_subdirectory(chap15/ex27)
add_subdirectory(chap15/ex28)
add_subdirectory(chap15/ex29)
add_subdirectory(chap15/ex30)
add_subdirectory(chap15/ex31)
add_subdirectory(chap15/ex34)
add_subdirectory(chap15/ex35)
add_subdirectory(chap15/ex36)
add_subdirectory(chap15/ex39)
add_subdirectory(chap15/ex41)
add_subdirectory(chap15/ex45)
add_subdirectory(chap15/ex46)
add_subdirectory(chap15/ex47)
add_subdirectory(chap15/ex48)
add_subdirectory(chap18/ex1)
add_subdirectory(chap18/ex2)
add_subdirectory(chap18/ex3)
add_subdirectory(chap18/ex4)
add_subdirectory(chap18/ex5)
add_subdirectory(chap18/ex6)
add_subdirectory(chap18/ex7)
add_subdirectory(chap18/ex8)
add_subdirectory(chap18/ex9)
add_subdirectory(chap18/ex10)
add_subdirectory(chap18/ex11)
add_subdirectory(chap18/ex12)
add_subdirectory(chap18/ex13)
add_subdirectory(chap18/ex14)
add_subdirectory(chap18/ex15)
add_subdirectory(chap18/ex16)
add_subdirectory(chap18/ex17)
add_subdirectory(chap18/ex18)
add_subdirectory(chap18/ex19)
add_subdirectory(chap18/ex20)
add_subdirectory(chap18/ex21)
add_subdirectory(chap18/ex22)
add_subdirectory(chap18/ex23)
add_subdirectory(chap18/ex24)
add_subdirectory(chap18/ex25)
add_subdirectory(chap18/ex26)
add_subdirectory(chap18/ex27)
add_subdirectory(chap18/ex28)
add_subdirectory(chap18/ex29)
add_subdirectory(chap18/ex30)
add_subdirectory(chap18/ex31)
add_subdirectory(chap18/ex32)
add_subdirectory(chap18/ex33)
add_subdirectory(chap18/ex34)
add_subdirectory(chap18/ex35)
add_subdirectory(chap19/ex1)
add_subdirectory(chap19/ex2)
add_subdirectory(chap19/ex3)
add_subdirectory(chap19/ex4)
add_subdirectory(chap19/ex5)
add_subdirectory(chap20/ex4)
add_subdirectory(chap20/ex5)
add_subdirectory(chap20/ex6)
add_subdirectory(chap20/ex7)
add_subdirectory(chap20/ex8)
add_subdirectory(chap20/ex10)
add_subdirectory(chap20/ex14)
add_subdirectory(chap20/ex16)
add_subdirectory(chap20/ex17)
add_subdirectory(chap20/ex18)
add_subdirectory(chap20/ex19)
add_subdirectory(chap20/ex20)
add_subdirectory(chap20/ex21)
add_subdirectory(chap20/ex22)
add_subdirectory(chap20/ex23)
add_subdirectory(chap20/ex24)
add_subdirectory(chap20/ex25)
add_subdirectory(chap20/ex27)

